package Day1;

import Kit.DBUtil;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class AddStudent {
    public static void main(String[] args) {
        Connection connect = DBUtil.getConnection();
        Scanner input =new Scanner(System.in);
        System.out.println("请输入新同学的姓名：");
        String name = input.next();
        System.out.println("请输入新同学的性别：");
        String sex = input.next();
        System.out.println("请输入新同学的电话：");
        String telephone = input.next();
        System.out.println("请输入新同学的QQ：");
        String qq = input.next();
        System.out.println("请输入新同学的微信：");
        String wechat = input.next();
        System.out.println("请输入新同学的年级号：");
        int grade = input.nextInt();
        System.out.println("请输入新同学的入学时间：");
        String enterdate = input.next();
        System.out.println("请输入新同学的籍贯：");
        String fromcity = input.next();
        System.out.println("请输入新同学的生日：");
        String birthday = input.next();
        System.out.println("请输入新同学的住址：");
        String adress = input.next();
        System.out.println("请输入新同学的密码：");
        String password = input.next();
        System.out.println("请输入新同学的身份证号码：");
        String idcard = input.next();
        String sql = "insert into s_student(sname,sex,telephone,qq,wechat,gradeid," +
                "enterdate,fromcity,birthday,address,pass,idcardpic) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps =null;
        try {
             ps = connect.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, sex);
            ps.setString(3, telephone);
            ps.setString(4, qq);
            ps.setString(5, wechat);
            ps.setInt(6, grade);
            ps.setDate(7, Date.valueOf(enterdate));
            ps.setString(8, fromcity);
            ps.setDate(9, Date.valueOf(birthday));
            ps.setString(10, adress);
            ps.setString(11, password);
            ps.setString(12, idcard);
            int i = ps.executeUpdate();
            if(i>0)
                System.out.printf("添加了%d行，添加成功！",i);
            else System.out.println("添加失败！");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtil.close(connect,ps,null);
        }
    }
}
